package com.iwhalecloud.uncc.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.properties.ArchiverProperties;
import com.iwhalecloud.uncc.web.dto.Result;
import com.iwhalecloud.uncc.web.service.InstantService;
import com.iwhalecloud.uncc.web.service.ScheduleService;
import com.iwhalecloud.uncc.web.service.WebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/13 11:30
 * @description 归档接口
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/archiver")
public class ArchiveController {

    @Autowired
    private InstantService instantService;

    @Autowired
    private ArchiverProperties archiverProperties;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 即时归档
     * @param jsonObject
     * @return Result
     */
    @RequestMapping("/instantArchiver")
    public Result instantArchiver(@RequestBody JSONObject jsonObject){
        boolean archiver = archiver(instantService, jsonObject);
        if(archiver){
            return Result.success(true);
        }else {
            return Result.fail("fail");
        }
    }

    /**
     * 定时归档
     * @param jsonObject
     * @return Result
     */
    @RequestMapping("/scheduleArchiver")
    public Result scheduleArchiver(@RequestBody JSONObject jsonObject){
        boolean archiver = archiver(scheduleService, jsonObject);
        if(archiver){
            return Result.success(true);
        }else {
            return Result.fail("fail");
        }
    }

    private boolean archiver(WebService webService ,JSONObject jsonObject){
        if (log.isInfoEnabled()) {
            log.info("Web发起一个归档请求,请求数据:"+jsonObject.toJSONString());
        }
        ArchiveParam archiveParam = JsonToArchiveParam(jsonObject);
        //ArchiverThreadLocal 传递 ArchiveParam
        ArchiverThreadLocal.setArchiveParam(archiveParam);
        switch (archiveParam.getTarget()){
            case ArchiverConstant.ALL:
                ArchiverThreadLocal.getArchiveParam().setEachInsLines(archiverProperties.getEachInsLines());
                webService.archiveAll();
                break;
            case ArchiverConstant.INS_COLLECT_TASK:
                webService.archiveInsCollectTask();
                break;
            case ArchiverConstant.INS_COLLECT_SERVICE:
                ArchiverThreadLocal.getArchiveParam().setEachInsLines(archiveParam.getNumber());
                webService.archiveInsCollectService();
                break;
            case ArchiverConstant.INS_COLLECT_SERVER:
                ArchiverThreadLocal.getArchiveParam().setEachInsLines(archiveParam.getNumber());
                webService.archiveInsCollectServer();
                break;
            default:
                System.out.println("default");
        }
        return true;
    }

    private ArchiveParam JsonToArchiveParam(JSONObject jsonObject) {
        List<String> dateTimeList = formatString(jsonObject.getJSONArray("date"));
        ArchiveParam archiveParam = new ArchiveParam();
        archiveParam.setTarget(jsonObject.getString("target"));
        archiveParam.setNumber(jsonObject.getIntValue("number"));
        archiveParam.setStartDate(dateTimeList.get(0));
        archiveParam.setEndDate(dateTimeList.get(1));
        archiveParam.setCron(jsonObject.getString("cron"));
        archiveParam.setIsDeleteOnline(jsonObject.getBoolean("isDelete"));
        return archiveParam;
    }

    private List<String> formatString(JSONArray date) {
        List<String> dateTimeList = new ArrayList<>();
        for (Object o : date){
            dateTimeList.add((String) o);
        }
        return dateTimeList;
    }

}
